package com.wangxin.study8.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @ClassName MatchStrTest
 * @Description 括号匹配
 * @Author jojo.wang
 * @Date 2019-05-17 18:40
 * @Version 1.0
 **/
public class MatchStrTest {
    private static final Map<Character,Character> barkets = new HashMap<>();
    static {
        barkets.put(')','(');
        barkets.put('}','{');
        barkets.put(']','[');
    }
    public static boolean matchStr(String str){
        if (null==str){
            return false;
        }
        Stack<Character> stack = new Stack<>();
        for (char s : str.toCharArray()) {
            if (barkets.containsValue(s)){
                stack.push(s);
            }else if (barkets.containsKey(s)){
                if (stack.isEmpty()||!stack.pop().equals(barkets.get(s))){
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        String a = "sdsd(sdsdsds}";
        System.out.println(matchStr(a));
    }
}
